package com.neusoft.nepa.controller;

import com.neusoft.nepa.UserLogin;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import java.io.IOException;
import java.util.ArrayList;

public class VukenAdminRegisterViewController {

    @FXML private TextField RegisterAge;
    @FXML private TextField RegisterName;
    @FXML private TextField RegisterPhoneNumber;
    @FXML private TextField RegisterSex;
    @FXML private TextField RegisterPassword;

    private UserLogin mainApp;

    public void setMainApp(UserLogin mainApp) {
        this.mainApp = mainApp;
    }
    @FXML
    private void handleRegister() throws IOException {
        boolean RegisterPass;
        if (RegisterName.getText().trim().isEmpty()||RegisterPassword.getText().trim().isEmpty()||RegisterPhoneNumber.getText().trim().isEmpty()||RegisterSex.getText().trim().isEmpty()||RegisterAge.getText().trim().isEmpty()) {
            mainApp.FormNotCorrect();
        } else if (!isMobilePhone(RegisterPhoneNumber.getText().trim())) {
            mainApp.PhoneNumberFormNotCorrect();
        } else if (!isPositiveInteger(RegisterAge.getText().trim()))
        {
            mainApp.FormNotCorrect();
        } else {
            RegisterPass = mainApp.includeStates(RegisterName.getText().trim(), RegisterPhoneNumber.getText().trim(), RegisterPassword.getText().trim(), RegisterSex.getText().trim(), Integer.parseInt(RegisterAge.getText()));
            if (RegisterPass) {
                mainApp.showLoginView();
            }
        }
    }
    @FXML
    private void handleLogin() throws IOException {
        mainApp.showLoginView();
    }

    private boolean isMobilePhone(String str) {
        ArrayList<String> arrayList = this.mainApp.getAdminKeys();
        return arrayList.contains(str);
    }

    public static boolean isPositiveInteger(String s) {
        if (s == null || s.isEmpty()) return false;
        for (char c : s.toCharArray()) {
            if (!Character.isDigit(c)) return false;
        }
        return !s.startsWith("0") && !s.equals("0");
    }
}